<?php

namespace App\Jobs\QiPao\Gift;

use App\Jobs\Job;
use App\Enums\RedisKeyEnum;
use App\Services\Tools\RedisService;
use App\Services\Tools\IdempotentConsume;
use Illuminate\Support\Facades\Log;

class RoomContributionJob extends Job
{
    protected $data;
    const jobName = 'RoomContributionJob';
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        try {
            $idempotentConsumeServer =  new IdempotentConsume();
            $data = $this->data;
            $uk = $data['id']??'';
            if($idempotentConsumeServer->checkIdempotentRedisKey(self::jobName,$uk)){
                $room_id = $this->data['room_id'] ?? 0;
                $user_id = $this->data['user_id'] ?? 0;
                $user_id_to = $this->data['gift_user'] ?? 0;
                $total_price = $this->data['total_price'] ?? 0;
                $total_price = intval($total_price);
                RedisService::setRoomUserContribution($room_id, $user_id, $user_id_to, $total_price);
            }
        } catch (\Exception $exception) {
            $idempotentConsumeServer->deleteIdempotentRedisKey(self::jobName,$uk);
            Log::error('RoomContributionJob:'.$exception->getMessage());
        }
    }
}
